package com.android.lovingpet.adopt.adoption.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.android.lovingpet.R;
import com.android.lovingpet.adapter.MyArrayAdapter;
import com.android.lovingpet.adopt.adoption.AdoptionPresenter;
import com.android.lovingpet.adopt.adoption.IAdoptionContract;
import com.android.lovingpet.adopt.adoption.adopt.view.PetDetailsActivity;
import com.android.lovingpet.adopt.adoption.publish.view.AddAdoptionActivity;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.common.EndLessOnScrollListener;
import com.android.lovingpet.util.LocationClient;
import com.android.lovingpet.util.SpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AdoptionActivity extends BaseActivity<IAdoptionContract.View, AdoptionPresenter> implements IAdoptionContract.View, View.OnClickListener, AdapterView.OnItemSelectedListener {

    @BindView(R.id.rv_pet)
    RecyclerView rvPet;
    @BindView(R.id.publish)
    Toolbar publish;
    @BindView(R.id.sp_address)
    Spinner spAddress;
    @BindView(R.id.sp_type)
    Spinner spType;
    @BindView(R.id.sp_gender)
    Spinner spGender;
    @BindView(R.id.sp_sort)
    Spinner spSort;
    @BindView(R.id.srl_rv)
    SwipeRefreshLayout srlRv;
    private RecyclerViewAdapter rvAdapter;
    private EndLessOnScrollListener endLessOnScrollListener;

    //上拉加载更多 有问题 直接加载一百个吧
    private final int LOAD_NUM=20;
    private int loadModel=0;
    private final int LOADMORE_ADDRESS=0;
    private final int LOADMORE_TYPE=1;
    private final int LOADMORE_GENDER=2;
    private final int LOADMORE_SORT=3;
    private int loadedCount=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adoption);
        ButterKnife.bind(this);
        initView();
        setTitle("领养宠物");
    }

    public void initView() {
        rvAdapter = new RecyclerViewAdapter(new ArrayList<>());
        rvPet.setAdapter(rvAdapter);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvPet.setLayoutManager(layoutManager);
        rvPet.setItemAnimator(new DefaultItemAnimator());
        rvAdapter.setOnItemClickListener(position -> {
            Pet selectedPet = rvAdapter.getData(position);
            Intent intent = new Intent(this, PetDetailsActivity.class);
            intent.putExtra("data", selectedPet);
            startActivity(intent);
        });

        //上拉加载更多  目前是重复加载数据吧 test
        endLessOnScrollListener = new EndLessOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                switch (loadModel)
                {
                    case LOADMORE_ADDRESS:
                        getPresenter().sortByAddress(spAddress.getSelectedItem().toString(),1,LOAD_NUM);
                        break;
                    case LOADMORE_TYPE:
                        getPresenter().sortByType(spType.getSelectedItem().toString(),1,LOAD_NUM);
                        break;
                    case LOADMORE_GENDER:
                        getPresenter().sortBySex(spGender.getSelectedItem().toString(),1,LOAD_NUM);
                        break;
                    case LOADMORE_SORT:
                        //假数据
                        break;
                }
            }
        };

        rvPet.setOnScrollListener(endLessOnScrollListener);

        //下拉刷新 整个加载思路不是很好等待重写
        srlRv.setOnRefreshListener(() -> {
            runOnUiThread(()->
            {
                rvAdapter.clear();
                loadedCount=0;
                getPresenter().sortByAddress("长沙",1,LOAD_NUM);
                endLessOnScrollListener.setPreviousTotal(0);
            });
        });

        spGender.setSelection(0);
        spType.setSelection(0);


        spAddress.setOnItemSelectedListener(this);
        spType.setOnItemSelectedListener(this);
        spGender.setOnItemSelectedListener(this);
        spSort.setOnItemSelectedListener(this);

        List<String> sort_list = new ArrayList<>();
        sort_list.add("最新");
        sort_list.add("附近");
        sort_list.add("排序");
        MyArrayAdapter sortAdapter = new MyArrayAdapter<>(this, android.R.layout.simple_spinner_item, sort_list);
        sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spSort.setAdapter(sortAdapter);
        spSort.setSelection(sort_list.size() - 1, true);

        srlRv.setRefreshing(true);
        //湖南省长沙市
        if (SpUtil.getCity() != null) {
            getPresenter().sortByAddress("长沙", 1, LOAD_NUM);
        } else {
            LocationClient.getLocation(null);
            getPresenter().sortByAddress("长沙", 1, LOAD_NUM);
        }

    }


    @Override
    public AdoptionPresenter createPresenter() {
        return new AdoptionPresenter();
    }

    @Override
    public IAdoptionContract.View createView() {
        return this;
    }

    @OnClick({R.id.publish})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.publish:
                startActivity(new Intent(this, AddAdoptionActivity.class));
                break;
        }
    }


    @Override
    public void next() {
        runOnUiThread(() -> {
            srlRv.setRefreshing(false);
        });
    }

    @Override
    public void addPet(Pet pet) {
        runOnUiThread(() -> {
            srlRv.setRefreshing(false);
            loadedCount+=1;
            rvAdapter.add(pet);
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            //写的如此low 日后再改
            case R.id.sp_sort:

                switch (position) {
                    //最新
                    case 0:

                        break;
                    //附近
                    case 1:

                        break;
                }
                break;
            case R.id.sp_address:
                if (position==0) {
                    //获取全部宠物

                }
                String address=parent.getItemAtPosition(position).toString();
                loadedCount=0;
                loadModel=LOADMORE_ADDRESS;
                rvAdapter.clear();
                getPresenter().sortByAddress(address, 1, LOAD_NUM);
                break;
            case R.id.sp_type:
                if (position==0) return;
                String type=parent.getItemAtPosition(position).toString();
                loadedCount=0;
                loadModel=LOADMORE_TYPE;
                rvAdapter.clear();
                getPresenter().sortByType(type,1,LOAD_NUM);
                break;
            case R.id.sp_gender:
                if (position==0) return;
                loadedCount=0;
                loadModel=LOADMORE_GENDER;
                rvAdapter.clear();
                getPresenter().sortBySex(String.valueOf(position-1),1,LOAD_NUM);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
